package com.ujiuye.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ujiuye.config.MessageConstant;
import com.ujiuye.config.Result;
import com.ujiuye.mapper.OrdersettingMapper;
import com.ujiuye.pojo.Ordersetting;
import com.ujiuye.service.IOrdersettingService;
import com.ujiuye.vo.DateVo;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单设置-比如设置今天可预约人数等信息 服务实现类
 * </p>
 *
 * @author zs
 * @since 2021-07-05
 */
@Service
public class OrdersettingServiceImpl extends ServiceImpl<OrdersettingMapper, Ordersetting> implements IOrdersettingService {
    @Resource
    private OrdersettingMapper ordersettingMapper;

    @Override
    public Result update(List<Ordersetting> list) {
        List<String> date = new ArrayList<>();
        for (Ordersetting ordersetting : list) {//要上传信息
            //构造条件对象
            QueryWrapper<Ordersetting> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("orderDate",ordersetting.getOrderdate());
            //将数据库的数据查出
            List<Ordersetting> ordersettingList = ordersettingMapper.selectList(queryWrapper);//数据库信息
            //判断数据库是否有这个时间的预约
            if (ordersettingList!=null && ordersettingList.size()>0){
                //有就查出每个对象的数据
                Ordersetting ordersetting1 = ordersettingList.get(0);
                //判断设置预约人数是否小于已经预约人数
                if (ordersetting1.getReservations()<ordersetting.getNumber()){
                    //小于则可以进行修改
                      ordersetting1.setNumber(ordersetting.getNumber());
                      ordersettingMapper.updateById(ordersetting1);
                }else {
                    //大于则创建一个集合将大于已经预约的时间存入集合
                    String errordate = LocalDateToString(ordersetting.getOrderdate());
                    date.add(errordate);
                }
            }else {
                //没有则添加数据
                ordersettingMapper.insert(ordersetting);
            }
        }
        //判断用于存设置预约错误的集合
        if (date.size()>0){
            return new Result(false,"部分设置成功,日期"+date+"设置失败预约数量大于可预约数量");
        }
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }

    @Override
    public List<DateVo> listDate(String date) {
        //创建一个集合来装数据库查出的信息
        List<DateVo> dateVoList = new ArrayList<>();
        //构造条件
        QueryWrapper<Ordersetting> queryWrapper = new QueryWrapper();
        //对参数进行拼接
       String beginTime = date+"-01";
       String endTime = date+"-31";
       //判断区间进行查询
       queryWrapper.between("orderDate",beginTime,endTime);
        List<Ordersetting> ordersettingList = ordersettingMapper.selectList(queryWrapper);
        //遍历查回来的数据
        for (Ordersetting ordersetting : ordersettingList) {
            //将数据存入对象中
            DateVo dateVo = new DateVo(ordersetting.getOrderdate().getDayOfMonth(),ordersetting.getNumber(),ordersetting.getReservations());
            //将对象存入集合
            dateVoList.add(dateVo);
        }
        return dateVoList;
    }

    @Override
    public Result updateNumber(Date date, Integer number) {
        //将date类型转为localdate类型
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        //构建条件
        QueryWrapper<Ordersetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderDate",localDate);
        //进行查询
        List<Ordersetting> ordersettingList = ordersettingMapper.selectList(queryWrapper);
        //判断查回来的集合是否有数据
        if (ordersettingList!=null && ordersettingList.size()>0){
            //有则取出来
            Ordersetting ordersetting = ordersettingList.get(0);
            //判断设置的预约量是否大于以预约量
            if (ordersetting.getReservations()<number){
                //没有大于就进行修改
                ordersetting.setNumber(number);
                ordersettingMapper.updateById(ordersetting);
            }else {
                //提醒客户
                return new Result(false,"预约数量以达到"+ordersetting.getReservations()+"个人");
            }

        }else {
            //没有添加一条
            Ordersetting ordersetting = new Ordersetting(null,localDate,number,0);
            ordersettingMapper.insert(ordersetting);
        }
        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }

    //用于localdate转换为string类型
    public String LocalDateToString(LocalDate localDate) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String dateStr = localDate.format(fmt);
        return dateStr;
    }
}
